package com.mbyte.easy.studentInfo.service.impl;

import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.status.entity.Status;
import com.mbyte.easy.status.service.IStatusService;
import com.mbyte.easy.studentInfo.entity.TStudent;
import com.mbyte.easy.studentInfo.mapper.TStudentMapper;
import com.mbyte.easy.studentInfo.service.UploadService;
import com.mbyte.easy.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UploadServiceImpl implements UploadService {
    @Autowired
    private TStudentMapper studentMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private IStatusService statusService;
    @Override
    public Integer read(List<List<String>> result) throws Exception {
        List<QcbyClass> list = qcbyClassService.list();
        List<Status> list1 = statusService.list();
        HashMap<String,Long> classMap = new HashMap<>();
        HashMap<String,Integer> statusMap = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            classMap.put(list.get(i).getClassName(),list.get(i).getId());
        }
        for (int i = 0; i < list1.size(); i++) {
            statusMap.put(list1.get(i).getStatus(), (int) list1.get(i).getId());
        }
        for (int i = 0; i < result.size(); i++) {
            if (i == 0) continue;
            List<String> studentInfo = result.get(i);
            //插入用户
            SysUser sysUser = new SysUser();
            sysUser.setAvailable(true);
            sysUser.setCreatetime(new Date());
            sysUser.setUpdatetime(new Date());
            sysUser.setUsername(studentInfo.get(0));
            sysUser.setPassword(Utility.QuickPassword(studentInfo.get(0)));
            String sexType = studentInfo.get(3);
            if(sexType.equals("男")){
                sysUser.setSexType(1);
            }else if (sexType.equals("女")){
                sysUser.setSexType(0);
            }
            if(userService.selectByUsername(sysUser.getUsername()) != null){
                throw new Exception(studentInfo.get(2)+"用户名重复！");
            }
            userService.insert(sysUser);
            SysUser sysUser1 = userService.selectByUsername(sysUser.getUsername());
            //插入学生
            TStudent student = new TStudent();
            student.setClassId(classMap.get(studentInfo.get(1)));
            student.setName(studentInfo.get(2));
            student.setSex(statusMap.get(studentInfo.get(4)));
            student.setAge(Integer.parseInt(studentInfo.get(5)));
            student.setPhone(studentInfo.get(6));
            student.setEmail(studentInfo.get(7));
            student.setUserId(sysUser1.getId());
            System.err.println("user信息"+sysUser);
            System.err.println("student信息"+student);
            studentMapper.insert(student);
        }
       
        return 1;
    }
}
